<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Content_model extends MY_Model
{
    function get($id, $gid)
    {
        $this->_db->select( $this->_table );
        $this->_db->where( '`id`=? and `gid`=? and `enable`=1 and `deleted`=0', array (
                $id,
                $gid 
        ) );
        $this->_db->limit( 1 );
        $result = $this->_db->fetch_row();
        if (! $result)
        {
            return array ();
        }
        $properties = json_decode( $result['properties'], true );
        if (is_array( $properties ))
        {
            $result += $properties;
        }
        return $result;
    }
    function get_near($id, $gid)
    {
        $this->_db->sql( 'SELECT rowno FROM (SELECT (@rowno:=@rowno+1) AS rowno,a.`id` FROM `_' . $this->_table . '` AS a,(SELECT @rowno:=0) AS b WHERE a.`gid`=? AND a.`enable`=1 AND a.`deleted`=0 ORDER BY a.`sort` DESC,a.`id` DESC) AS c WHERE `id`=? LIMIT 1', array (
                $gid,
                $id 
        ) );
        $rowno = $this->_db->fetch_cell();
        if (empty( $rowno ))
        {
            return array (
                    'prev' => null,
                    'next' => null 
            );
        }
        $this->_db->sql( 'SELECT * FROM (SELECT (@rowno:=@rowno+1) AS rowno,a.`id`,a.`title`,a.`subtitle`,a.`uri` FROM `_' . $this->_table . '` AS a,(SELECT @rowno:=0) AS b WHERE a.`gid`=? AND a.`enable`=1 AND a.`deleted`=0 ORDER BY a.`sort` DESC,a.`id` DESC) AS c WHERE `rowno`>=? AND `id`<>? LIMIT 2', array (
                $gid,
                $rowno - 1,
                $id 
        ) );
        $rows = $this->_db->fetch_all();
        
        if ($rowno == 1)
        {
            return array (
                    'prev' => null,
                    'next' => isset( $rows[0] ) ? $rows[0] : null 
            );
        }
        return array (
                'prev' => $rows[0],
                'next' => isset( $rows[1] ) ? $rows[1] : null 
        );
    }
    function get_list_count($cid, $gid)
    {
        $this->_db->select( $this->_table, 'count(*)' );
        if (is_array( $cid ))
        {
            $parameter = $cid;
            $parameter[] = $gid;
            $this->_db->where( '`cid` in(?) and `gid`=? and `enable`=1 and `deleted`=0', $parameter );
        }
        else
        {
            $this->_db->where( '`cid`=? and `gid`=? and `enable`=1 and `deleted`=0', array (
                    $cid,
                    $gid 
            ) );
        }
        return ( int ) $this->_db->fetch_cell();
    }
    function get_list($cid, $gid, $length = 10, $begin = 0, $desc = false)
    {
        $this->_db->select( $this->_table, '`id`,`flag`,`img_cover`,`uri`,`title`,`subtitle`,`summary`,`price`,`price_old`,`file_type`,`file_size`,`properties`,`add_time`' );
        if (is_array( $cid ))
        {
            $parameter = $cid;
            $parameter[] = $gid;
            $this->_db->where( '`cid` in(?) and `gid`=? and `enable`=1 and `deleted`=0', $parameter );
        }
        else
        {
            $this->_db->where( '`cid`=? and `gid`=? and `enable`=1 and `deleted`=0', array (
                    $cid,
                    $gid 
            ) );
        }
        if ($desc)
        {
            $this->_db->order( '`sort` desc,`id` desc' );
        }
        else
        {
            $this->_db->order( '`sort` desc,`id`' );
        }
        $this->_db->limit( $length, $begin );
        $result = $this->_db->fetch_all();
        
        if (! $result)
        {
            return array ();
        }
        foreach ( $result as &$row )
        {
            $properties = json_decode( $row['properties'], true );
            if (is_array( $properties ))
            {
                $row += $properties;
            }
        }
        return $result;
    }
    function get_flag_list($flag, $cid, $length = 10)
    {
        $this->_db->select( $this->_table, '`id`,`flag`,`img_cover`,`uri`,`title`,`subtitle`,`summary`,`price`,`price_old`,`file_type`,`file_size`,`add_time`' );
        $this->_db->where( '`cid`=? and `flag`&' . $flag . ' and `enable`=1 and `deleted`=0', $cid );
        $this->_db->order( '`sort` desc,`id` desc' );
        $this->_db->limit( $length );
        return $this->_db->fetch_all();
    }
    function search_count($key, $gid)
    {
        $this->_db->select( $this->_table, 'count(*)' );
        $this->_db->where( '`title` like ? and `gid`=? and `enable`=1 and `deleted`=0', array (
                "%$key%",
                $gid 
        ) );
        return ( int ) $this->_db->fetch_cell();
    }
    function search($key, $gid, $length = 10, $begin = 0)
    {
        $this->_db->select( $this->_table, '`id`,`flag`,`img_cover`,`uri`,`title`,`subtitle`,`summary`,`price`,`price_old`,`file_type`,`file_size`,`add_time`' );
        $this->_db->where( '`title` like ? and `gid`=? and `enable`=1 and `deleted`=0', array (
                "%$key%",
                $gid 
        ) );
        $this->_db->order( '`sort` desc,`id` desc' );
        $this->_db->limit( $length, $begin );
        return $this->_db->fetch_all();
    }
    // 导航二级菜单（多组一次取出）
    function get_sub_menus($cids)
    {
        $result = array ();
        if (empty( $cids ))
        {
            return $result;
        }
        $this->_db->select( $this->_table, '`id`,`cid`,`uri`,`title`,`subtitle`' );
        $this->_db->where( 'cid in(?) and `enable`=1 and `deleted`=0', $cids );
        $this->_db->order( '`cid`,`sort` desc,`id`' );
        $contents = $this->_db->fetch_all();
        foreach ( $contents as &$item )
        {
            $result[$item['cid']][] = &$item;
        }
        return $result;
    }
}